﻿using ETModel;
using System;
using UnityEngine;

namespace ETHotfix {
    [MessageHandler]
    public class M2C_UnitInfoSyncHandler : AMHandler<M2C_UnitInfoSync> {


        protected UnitsComponent _GameUnitComponent = null;
        /// <summary>
        /// 获取当前场景加载的单位信息同步客户端组件
        /// </summary>
        /// <returns></returns>
        public UnitsComponent GetGameUnitComponent() {
            if (_GameUnitComponent == null) {
                _GameUnitComponent = Game.Scene.GetComponent<UnitsComponent>();
            }
            if (_GameUnitComponent == null || _GameUnitComponent.IsDisposed) return null;
            return _GameUnitComponent;
        }

        protected override void Run(ETModel.Session session, M2C_UnitInfoSync message) {
            UnitsComponent unitComponent = GetGameUnitComponent();
            foreach (var unitSyncInfo in message.UnitSyncInfos) {
                var unit = unitComponent.Get(unitSyncInfo.Id) as GameUnit;
                if (unit == null) {
                    continue;
                }
                //todo:zum:需要插值,pos和q的,demo演示随便拉~
                unit.SetCenterPosition(unitSyncInfo.GetCenterPosition());
                unit.Rotation = unitSyncInfo.GetRotation();
                unit.Scale = unitSyncInfo.GetScale();
            }

        }
    }
}
